package com.jason.platform.supervision.activity;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;
import com.hzlj.supervision.changsha.R;
import com.jason.platform.supervision.base.Constant;
import com.jason.platform.supervision.beans.*;
import com.jason.platform.supervision.task.QueryHistoryCountTask;
import com.jason.platform.supervision.task.QueryHistoryTask;
import com.jason.platform.supervision.task.Task;
import com.jason.platform.supervision.task.TaskListener;
import com.jason.platform.supervision.utils.DateUtils;
import com.jason.platform.supervision.widght.LoadingDialog;
import com.jason.platform.supervision.widght.TitleBar;

import java.util.Calendar;

/**
 * @ClassName: PlayBackDetailActivity
 * @Description: 轨迹回放详情
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2015-7-25 09:47:20
 */
public class PlayBackDetailActivity extends BaseActivity implements View.OnClickListener,TaskListener {

    private static final String TAG = PlayBackDetailActivity.class.getSimpleName();

    private MyHandler<PlayBackDetailActivity> mHandler = new MyHandler<PlayBackDetailActivity>(this);

    public static final String KEY_LOCATE = "key_locate";

    //准备对话框Id
    private static final int DATE_BEGIN_DIALOG_ID = 0;
    private static final int DATE_END_DIALOG_ID = 1;

    private TitleBar mTitleBar;

    //内容
    private TextView mNameTextView, mOrangeTextView, mBeginTextView, mEndTextView, mLocateCountTextView, mNormalCountTextView, mBoundsCountTextView;
    private Button mSearchButton, mPlaybackButton;

    private String mBeginDate;
    private String mEndDate;

    private int mYear, mMonth, mDay;

    private int mDateDialogType = DATE_BEGIN_DIALOG_ID;

    private LocateBean mLocateBean;
    private String mRymcId;
    private LocateCountBean mLocateCountBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_playback_detail);
        super.onCreate(savedInstanceState);
        mTitleBar = (TitleBar) findViewById(R.id.title_bar);
        mTitleBar.bindActivity(this);
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        mNameTextView = (TextView) findViewById(R.id.playback_detail_name);
        mOrangeTextView = (TextView) findViewById(R.id.playback_detail_orange);
        mBeginTextView = (TextView) findViewById(R.id.playback_detail_begin);
        mOrangeTextView = (TextView) findViewById(R.id.playback_detail_orange);
        mEndTextView = (TextView) findViewById(R.id.playback__detail_end);
        mLocateCountTextView = (TextView) findViewById(R.id.playback_detail_locate_count);
        mNormalCountTextView = (TextView) findViewById(R.id.playback_detail_normal_count);
        mBoundsCountTextView = (TextView) findViewById(R.id.playback_detail_bounds_count);
        mSearchButton = (Button) findViewById(R.id.playback_detail_search);
        mPlaybackButton = (Button) findViewById(R.id.playback_detail_playback);
        mBeginTextView.setOnClickListener(this);
        mEndTextView.setOnClickListener(this);
        mSearchButton.setOnClickListener(this);
        mPlaybackButton.setOnClickListener(this);
        mBeginTextView.setText(DateUtils.converDatLongToString(System.currentTimeMillis(), DateUtils.DATE_YMD));
        mEndTextView.setText(DateUtils.converDatLongToString(System.currentTimeMillis(), DateUtils.DATE_YMD));
    }


    @Override
    protected void initData() {
        super.initData();
        mLocateBean = (LocateBean) getIntent().getSerializableExtra(KEY_LOCATE);
        if (mLocateBean != null){
            mRymcId = mLocateBean.getElectronicsId();
        }
        initDate();
        updateData();
    }

    private void initDate() {
        //得到当前日期
        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);
    }

    @Override
    protected void initLoadingDialog() {
        super.initLoadingDialog();
        mLoadingDialog = new LoadingDialog(this);
        mLoadingDialog.setIsOut(false);
        mLoadingDialog.setMessage("加载中");
    }

    private boolean getSearchValue() {
        mBeginDate = mBeginTextView.getText().toString();
        mEndDate = mEndTextView.getText().toString();
        if (DateUtils.daysBetween(mBeginDate,DateUtils.converDatLongToString(System.currentTimeMillis(), DateUtils.DATE_YMD)) < 0 ||
                DateUtils.daysBetween(mEndDate,DateUtils.converDatLongToString(System.currentTimeMillis(), DateUtils.DATE_YMD)) < 0){
            Toast.makeText(this,"查询时间不能超过当天",Toast.LENGTH_SHORT).show();
            return false;
        }
        if (DateUtils.daysBetween(mBeginDate,mEndDate) <= 30 && DateUtils.daysBetween(mBeginDate,mEndDate) >= 0){
            return true;
        }
        Toast.makeText(this,"结束日期和开始日期不能超过30天",Toast.LENGTH_SHORT).show();
        return false;
    }

    private void updateData() {
        if (mLocateBean == null){
            return;
        }
        mNameTextView.setText(mLocateBean.getElectronicsName());
        mOrangeTextView.setText(mLocateBean.getOrganName());
    }

    private void updateCountData(){
        if (mLocateCountBean == null){
            return;
        }
        mLocateCountTextView.setText(String.valueOf(mLocateCountBean.getLocateCount()));
        mNormalCountTextView.setText(String.valueOf(mLocateCountBean.getZhengchangCount()));
        mBoundsCountTextView.setText(String.valueOf(mLocateCountBean.getYuejieCount()));
    }

    //准备日期设置监听器
    private DatePickerDialog.OnDateSetListener dsl = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                              int dayOfMonth) {
            mYear = year;
            mMonth = monthOfYear;
            mDay = dayOfMonth;
            String month = String.valueOf(mMonth + 1);
            String day = String.valueOf(mDay);
            if (month.length() == 1) {
                month = "0" + month;
            }
            if (day.length() == 1) {
                day = "0" + day;
            }

            if (mDateDialogType == DATE_BEGIN_DIALOG_ID) {

                mBeginTextView.setText(mYear + "-" + month + "-" + day);
            } else if (mDateDialogType == DATE_END_DIALOG_ID) {
                mEndTextView.setText(mYear + "-" + month + "-" + day);
            }
        }
    };

    @Override
    protected Dialog onCreateDialog(int id) {
        // TODO Auto-generated method stub
        if (id == DATE_BEGIN_DIALOG_ID) {
            //把Activity上下文、日期设置监听器、以及日期的年月日都作为参数传给那个新成立的对话框组件
            return new DatePickerDialog(this, dsl, mYear, mMonth, mDay);
        } else if (id == DATE_END_DIALOG_ID) {
            //把Activity上下文、日期设置监听器、以及日期的年月日都作为参数传给那个新成立的对话框组件
            return new DatePickerDialog(this, dsl, mYear, mMonth, mDay);
        }
        return null;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.playback_detail_search) {
            //TODO 调用接口，进行搜索
            if (getSearchValue()){
                mLoadingDialog.show();
                getSearchValue();
                QueryHistoryCountTask task = new QueryHistoryCountTask(this);
                task.type = Constant.Locate.GET_HISTORY_COUNT;
                task.mParams.put("rymcId", mRymcId);
                task.mParams.put("startTime", mBeginDate);//开始时间
                task.mParams.put("endTime", mEndDate);//结束时间
                task.execute();
            }

        } else if (v.getId() == R.id.playback_detail_playback) {
            //TODO 回放
            mLoadingDialog.show();
            getSearchValue();
            QueryHistoryTask task = new QueryHistoryTask(this);
            task.type = Constant.Locate.GET_HISTORY;
            task.mParams.put("rymcId", mRymcId);
            task.mParams.put("startTime", mBeginDate);//开始时间
            task.mParams.put("endTime", mEndDate);//结束时间
            task.execute();
        } else if (v.getId() == R.id.playback_detail_begin) {
            //TODO 开始时间
            mDateDialogType = DATE_BEGIN_DIALOG_ID;
            showDialog(DATE_BEGIN_DIALOG_ID);
        } else if (v.getId() == R.id.playback__detail_end) {
            //TODO 结束时间
            mDateDialogType = DATE_END_DIALOG_ID;
            showDialog(DATE_END_DIALOG_ID);
        }
    }

    @Override
    public void onTaskResult(Task task) {
        Message msg = new Message();
        msg.obj = task;
        mHandler.sendMessage(msg);
    }

    @Override
    void handleMessage(BaseActivity activity, Message msg) {
        Task task = (Task) msg.obj;
        if (task.type == Constant.Locate.GET_LOCATE_LIST) {
            if ( mLoadingDialog != null){
                mLoadingDialog.dismiss();
            }
            if (task.isSuccess) {
                LocateResp resp = (LocateResp) task.resData;
                mLocateBean = resp.getValues().get(0);
                updateData();
            }else {
                Toast.makeText(this, task.rspDesc, Toast.LENGTH_SHORT).show();
            }
        }else if (task.type == Constant.Locate.GET_HISTORY_COUNT){
            if ( mLoadingDialog != null){
                mLoadingDialog.dismiss();
            }
            if (task.isSuccess) {
                HistoryCountResp resp = (HistoryCountResp) task.resData;
                mLocateCountBean = resp.getValues().get(0);
                updateCountData();
            }else {
                Toast.makeText(this, task.rspDesc, Toast.LENGTH_SHORT).show();
            }
        }else if (task.type == Constant.Locate.GET_HISTORY){
            if ( mLoadingDialog != null){
                mLoadingDialog.dismiss();
            }
            if (task.isSuccess) {
                HistoryResp resp = (HistoryResp) task.resData;
                //TODO 进入地图
                Intent intent = new Intent(PlayBackDetailActivity.this,BaiduMapActivity.class);
                intent.setAction(BaiduMapActivity.ACTION_LOCATE_PLAYBACK);
                intent.putExtra(BaiduMapActivity.KEY_LOCATE_PLAYBACK,resp);
                startActivity(intent);
            }else {
                Toast.makeText(this, task.rspDesc, Toast.LENGTH_SHORT).show();
            }
        }
    }
}
